package com.yunji.storage.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunji.common.annotation.EventRecord;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.EventBusinessTypeConstant;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.bo.AllocateListQueryBO;
import com.yunji.storage.bo.AllocateRemoveBO;
import com.yunji.storage.bo.AllocateSelectQueryBO;
import com.yunji.storage.dao.IAllocateTicketDao;
import com.yunji.storage.domain.AllocateTicketDO;
import com.yunji.storage.enums.AllocateStatus;
import com.yunji.storage.constant.StorageEventTypeConstant;
import com.yunji.storage.mapper.AllocateTicketMapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Repository
public class AllocateTicketDaoImpl extends BaseDaoImpl<AllocateTicketMapper, AllocateTicketDO> implements IAllocateTicketDao {

    @Override
    public List<AllocateTicketDO> search(AllocateListQueryBO bo) {
        return list(new LambdaQueryWrapper<AllocateTicketDO>()
                .eq(BeanUtils.isNotNull(bo.getBelongId()), AllocateTicketDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getAllocateStatus()), AllocateTicketDO::getAllocateStatus, bo.getAllocateStatus()));
    }

    @Override
    public AllocateTicketDO select(AllocateSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<AllocateTicketDO>()
                .eq(AllocateTicketDO::getAllocateCode, bo.getAllocateCode()));
    }

    @Override
    public boolean deleteByTicket(AllocateRemoveBO bo) {
        return remove(new LambdaQueryWrapper<AllocateTicketDO>()
                .eq(AllocateTicketDO::getBelongId, bo.getBelongId())
                .eq(AllocateTicketDO::getAllocateStatus, bo.getAllocateStatus()));
    }

    @Override
    public boolean deleteDraft(Long... allocateIds) {
        return remove(new LambdaQueryWrapper<AllocateTicketDO>()
                .in(AllocateTicketDO::getAllocateId, Arrays.asList(allocateIds))
                .eq(AllocateTicketDO::getAllocateStatus, AllocateStatus.DRAFT.getCode()));
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_ALLOCATE,eventType = StorageEventTypeConstant.EVENT_TYPE_STORAGE)
    public boolean create(AllocateTicketDO allocateTicket) {
        return save(allocateTicket);
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_ALLOCATE,eventType = StorageEventTypeConstant.EVENT_TYPE_STORAGE)
    public boolean updateTicket(AllocateTicketDO allocateTicket) {
        return updateById(allocateTicket);
    }
}
